package com.glo.spreadsheet2db.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.glo.spreadsheet2db.model.Spreadsheet;
import com.glo.spreadsheet2db.services.Spreadsheet2DbService;

//@ManagedBean(name = "importer")
//@RequestScoped
@Controller
@Scope("Request")
public class ImportController
{

	@Autowired
	private Spreadsheet2DbService spreadsheet2DbService; 
	
    //properties
    private String userName = "fernando.sosa@globallogic.com";
    private String password;
    private String sheetName = "prueba";

	private Spreadsheet spreadsheet;
	
	private String errors = "";

    public String importSheet() {
    	try {
    		if ( !spreadsheet2DbService.existsSpreadsheet(userName, password, sheetName) ) {
    			errors = "The spreadsheet '" + sheetName + "' doesn't exists for user '"+ userName +"'";
    		}
			spreadsheet = spreadsheet2DbService.importAndSaveSpreadsheet(userName, password, sheetName);
			if ( spreadsheet != null ) {
				return "data.xhtml";
			}
		} catch (Exception e) {
			errors = e.getMessage();
		}
    	
    	return null;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String name) {
        this.userName = name;
    }

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public String getErrors() {
		return errors;
	}

	public Spreadsheet getSpreadsheet() {
		return spreadsheet;
	}

	public String[][] getRows() {
		return rows;
	}

	public void setRows(String[][] rows) {
		this.rows = rows;
	}

	public String[] getHeader() {
		return header;
	}

	public void setHeader(String[] header) {
		this.header = header;
	}

	private String[][] rows = new String[][]{ 
					new String[]{"A","B","C"},
					new String[]{"A1","B1","C1"},
					new String[]{"A2","B2","C2"},
					new String[]{"A3","B3","C3"} };
	
	private String[] header = 
			new String[]{"A","B","C"};

}
